package com.southwind.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.southwind.entity.ProductCategory;
import com.southwind.entity.ProductInfo;
import com.southwind.mapper.ProductInfoMapper;
import com.southwind.service.IProductInfoService;
import com.southwind.vo.ProductInfoPageVO;
import com.southwind.vo.ProductInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-04-18
 */
@Service
@Slf4j
public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, ProductInfo> implements IProductInfoService {

    @Autowired
    private ProductInfoMapper productInfoMapper;

    /**
     * 商品减库存
     * @param id
     * @param quantity
     * @return
     */
    @Override
    public Boolean subStock(Integer id, Integer quantity) {
        ProductInfo productInfo = productInfoMapper.selectById(id);
        // 判断商品是否存在
        if (productInfo == null) {
            log.info("商品不存在，id={}", id);
            throw new RuntimeException("商品不存在！");
        }
        Integer productStock = productInfo.getProductStock();
        // 判断当前库存是否足够
        int result = productStock - quantity;
        if (result < 0) {
            log.info("库存不足，id={}，stock={}", id, productStock);
            throw new RuntimeException("库存不足！");
        }
        productInfo.setProductStock(result);
        // 存入数据库
        int updateRow = productInfoMapper.updateById(productInfo);
        return updateRow == 1;
    }

    @Override
    public ProductInfoPageVO productInfoPageVO(Long page, Long size) {
        Page<ProductInfo> productInfoPage = new Page<>(page, size);
        productInfoMapper.selectPage(productInfoPage, null);
        List<ProductInfo> records = productInfoPage.getRecords();
        List<ProductInfoVO> productInfoVOList = new ArrayList<>();
        for (ProductInfo record : records) {
            ProductInfoVO productInfoVO = new ProductInfoVO();
            BeanUtils.copyProperties(record, productInfoVO);
            // 赋值status    1正常，0下架
            productInfoVO.setStatus(record.getProductStatus() == 1);
            String categoryName = productInfoMapper.selectCategoryNameByCategoryType(record.getCategoryType());
            // 赋值categoryName
            productInfoVO.setCategoryName(categoryName);
            productInfoVOList.add(productInfoVO);
        }
        // 封装ProductInfoPageVO
        ProductInfoPageVO productInfoPageVO = new ProductInfoPageVO();
        productInfoPageVO.setSize(productInfoPage.getSize());
        productInfoPageVO.setTotal(productInfoPage.getTotal());
        productInfoPageVO.setContent(productInfoVOList);
        return productInfoPageVO;
    }

    @Override
    public ProductInfoVO findById(Integer id) {
        ProductInfo productInfo = productInfoMapper.selectById(id);
        ProductInfoVO productInfoVO = new ProductInfoVO();
        if (productInfo == null) {
            return productInfoVO;
        }
        BeanUtils.copyProperties(productInfo, productInfoVO);
        // 赋值status    1正常，0下架
        productInfoVO.setStatus(productInfo.getProductStatus() == 1);
        // 赋值category
        ProductCategory category = new ProductCategory();
        category.setCategoryType(productInfo.getCategoryType());
        productInfoVO.setCategory(category);
        return productInfoVO;
    }
}
